﻿namespace AnyOffice.Components
{
    using Microsoft.ApplicationBlocks.Data;
    using System;
    using System.Data;
    using System.Data.SqlClient;
    using System.Text;

    public class StringHelper
    {
        public static string FormatSize(long size)
        {
            if (size > 0 && size < 0x400) return "1K";
            if (size < 0x100000) return (Convert.ToString((long) (size / 0x400)) + "K");
            float num = Convert.ToSingle(size) / 1048576f;
            return (num.ToString("0.00") + "M");
        }

        public static string GetUserNames(SqlConnection conn, string userId)
        {
            StringBuilder builder = new StringBuilder();
            SqlParameter[] parameterArray = new SqlParameter[] { new SqlParameter("@UserIds", SqlDbType.NVarChar, 0xfa0) };
            parameterArray[0].Value = userId;
            string str = "SELECT [Users].[UserName],[Department].[ParentDept],[Department].[DeptName] FROM [dbo].[Users],[dbo].[Department] WHERE [Users].[DeptId] = [Department].[DeptId] AND CHARINDEX(',' + [Users].[UserId] + ',',',' + @UserIds + ',') > 0";
            DataTable table = SqlHelper.ExecuteDataset(conn, CommandType.Text, str, parameterArray).Tables[0];
            if (table.Rows.Count > 0)
            {
                for (int i = 0; i < table.Rows.Count; i++)
                {
                    builder.Append("<u title=\"部门：");
                    string deptName = table.Rows[i]["DeptName"].ToString();
                    if (Convert.ToInt32(table.Rows[i]["ParentDept"].ToString()) != 0) PrentDeptName(Convert.ToInt32(table.Rows[i]["ParentDept"].ToString()), ref deptName);
                    builder.Append(deptName);
                    builder.Append("\" style=\"cursor:hand\">");
                    builder.Append(table.Rows[i]["UserName"].ToString());
                    builder.Append("</u>");
                    if (i < table.Rows.Count - 1) builder.Append(",");
                }
            }
            return builder.ToString();
        }

        public static string GeReplyNames(SqlConnection conn, string userId)
        {
            StringBuilder builder = new StringBuilder();
            SqlParameter[] parameterArray = new SqlParameter[] { new SqlParameter("@UserIds", SqlDbType.NVarChar, 0xfa0) };
            parameterArray[0].Value = userId;
            string str = "SELECT [Users].[UserName],[Department].[ParentDept],[Department].[DeptName] FROM [dbo].[Users],[dbo].[Department] WHERE [Users].[DeptId] = [Department].[DeptId] AND CHARINDEX(',' + [Users].[UserId] + ',',',' + @UserIds + ',') > 0";
            DataTable table = SqlHelper.ExecuteDataset(conn, CommandType.Text, str, parameterArray).Tables[0];
            if (table.Rows.Count > 0)
            {
                for (int i = 0; i < table.Rows.Count; i++)
                { 
                    builder.Append(table.Rows[i]["UserName"].ToString());
                    
                    if (i < table.Rows.Count - 1) builder.Append(",");
                }
            }
            return builder.ToString();
        }

        public static void PrentDeptName(int deptId, ref string deptName)
        {
            DataRow[] rowArray = ApplicationCache.Department.Select("DeptId=" + deptId.ToString());
            if (rowArray.Length > 0)
            {
                deptName = rowArray[0]["DeptName"] + "/" + deptName;
                if (Convert.ToInt32(rowArray[0]["ParentDept"].ToString()) != 0) PrentDeptName(Convert.ToInt32(rowArray[0]["ParentDept"].ToString()), ref deptName);
            }
        }
    }
}

